package com.baichuan.util;

import com.itextpdf.text.Document;
import com.itextpdf.text.Image;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfWriter;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @Auther: leo
 * @Date: 2022/6/28 12:54
 * @Description: 图片转PDF
 */
public class Convert2Pdf {
    public static InputStream convert(List<String> sourceList) {
        Document document = new Document();
        //设置文档页边距
        document.setMargins(0,0,0,0);
        ByteArrayOutputStream fos = null;
        try {

            fos = new ByteArrayOutputStream();
            PdfWriter.getInstance(document, fos);
            //打开文档
            document.open();

            //新建一页添加图片
            document.newPage();
            for (String source : sourceList) {
                //获取图片的宽高
                Image image = Image.getInstance(source);
                float imageHeight=image.getScaledHeight();
                float imageWidth=image.getScaledWidth();
                //设置页面宽高与图片一致
                Rectangle rectangle = new Rectangle(imageWidth, imageHeight);
                document.setPageSize(rectangle);
                //图片居中
                image.setAlignment(Image.ALIGN_CENTER);

                document.add(image);
            }
            return new ByteArrayInputStream(fos.toByteArray());
        } catch (Exception ioe) {
            System.out.println(ioe.getMessage());
        } finally {
            //关闭文档
            document.close();
            try {
                fos.flush();
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
    public static InputStream convert(String source) {
        Document document = new Document();
        //设置文档页边距
        document.setMargins(0,0,0,0);
        ByteArrayOutputStream fos = null;
        try {

            fos = new ByteArrayOutputStream();
            PdfWriter.getInstance(document, fos);
            //打开文档
            document.open();

            //新建一页添加图片
            document.newPage();
            //获取图片的宽高
            Image image = Image.getInstance(source);
            float imageHeight=image.getScaledHeight();
            float imageWidth=image.getScaledWidth();
            //设置页面宽高与图片一致
            Rectangle rectangle = new Rectangle(imageWidth, imageHeight);
            document.setPageSize(rectangle);
            //图片居中
            image.setAlignment(Image.ALIGN_CENTER);

            document.add(image);
            return new ByteArrayInputStream(fos.toByteArray());
        } catch (Exception ioe) {
            System.out.println(ioe.getMessage());
        } finally {
            //关闭文档
            document.close();
            try {
                fos.flush();
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
    public static void main(String[] args) {
        List<String> sourceList = new ArrayList<>();
        sourceList.add("https://zzsourcecar.oss-cn-hangzhou.aliyuncs.com/car/archives/220628/51b52e2a9e234ee781656155d32504c2o.jpg?Expires=3237510601&OSSAccessKeyId=LTAI4GBcZtMnrVQ72WG22fri&Signature=A9N3dUJF8ag7V5CS%2F29E%2BwPvJvY%3D");
        sourceList.add("https://zzsourcecar.oss-cn-hangzhou.aliyuncs.com/car/archives/220628/df78137f9093432482287cdbbc0c1e75o.jpg?Expires=3237510601&OSSAccessKeyId=LTAI4GBcZtMnrVQ72WG22fri&Signature=emxT3afCW%2FLlR%2FQVbdqU1ZMu93E%3D");
        String target = "E:/usr/1.pdf";
        System.out.println(convert(sourceList));
    }
}